/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.service.impl;

import cn.hutool.core.date.DateUtil;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.dto.RepeatFaultPlanListDTO;
import org.springblade.modules.admin.entity.ElevatorRepeatFaultConfig;
import org.springblade.modules.admin.entity.ElevatorWxNumber;
import org.springblade.modules.admin.entity.MaintainPlanContent;
import org.springblade.modules.admin.service.IMaintainPlanContentService;
import org.springblade.modules.admin.vo.ElevatorRepeatFaultConfigVO;
import org.springblade.modules.admin.mapper.ElevatorRepeatFaultConfigMapper;
import org.springblade.modules.admin.service.IElevatorRepeatFaultConfigService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 电梯维修故障次数标准表 服务实现类
 *
 * @author ysq
 * @since 2025-02-25
 */
@AllArgsConstructor
@Service
public class ElevatorRepeatFaultConfigServiceImpl extends BaseServiceImpl<ElevatorRepeatFaultConfigMapper, ElevatorRepeatFaultConfig> implements IElevatorRepeatFaultConfigService {

	@Override
	public IPage<ElevatorRepeatFaultConfigVO> selectElevatorRepeatFaultConfigPage(IPage<ElevatorRepeatFaultConfigVO> page, ElevatorRepeatFaultConfigVO elevatorRepeatFaultConfig) {
		return page.setRecords(baseMapper.selectElevatorRepeatFaultConfigPage(page, elevatorRepeatFaultConfig));
	}

	@Override
	public boolean init() {
		List<ElevatorRepeatFaultConfig> configs = new ArrayList<>();
		for (int i = 1; i < 7; i++) {
			ElevatorRepeatFaultConfig config = new ElevatorRepeatFaultConfig();
			config.setLevel(i);
			config.setRepetitionCounts(7-i);
			if(i > 3){
				config.setDays(30);
			}else {
				config.setDays(60);
			}
			if(i < 5){
				config.setOvertimeDay(2);
			}
			config.setTypeDesc(config.getDays()+"天"+config.getRepetitionCounts()+"次");
			configs.add(config);
		}
		return super.saveBatch(configs);
	}

	@Override
	public ElevatorRepeatFaultConfig getFaultLevelByElevator(Long elevatorId) {
		List<ElevatorRepeatFaultConfig> configs = super.list();
		if(Func.isNotEmpty(configs)){
			int maxDays = configs.stream().mapToInt(ElevatorRepeatFaultConfig::getDays).max().getAsInt();
			String startDate = DateUtil.format(DateUtil.offsetDay(DateUtil.date(),-maxDays),"yyyy-MM-dd");
			List<RepeatFaultPlanListDTO> allPlanList = baseMapper.selectRepeatFaultPlanList(elevatorId,startDate);
			configs = configs.stream().filter(e -> {
				long count = allPlanList.stream().filter(k -> k.getDays() <= e.getDays()).count();
				if(count >= e.getRepetitionCounts()){
					return Boolean.TRUE;
				}
				return Boolean.FALSE;
			}).sorted(Comparator.comparing(ElevatorRepeatFaultConfig::getLevel)).collect(Collectors.toList());
			if(Func.isNotEmpty(configs)){
				return configs.get(0);
			}
		}
		return null;
	}

}
